/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "spatial_fuzzer.h"

#include <iostream>
#include "securec.h"
#include "spatial_awareness_mgr_client.h"
#include "spatial_location_callback_stub.h"

using namespace std;
using namespace OHOS::Msdp;

constexpr int FUZZ_DATA_LEN = 8;
constexpr int FUZZ_FST_DATA = 0;
constexpr int MAX_SIZE = 64;
constexpr int ZERO_DATA = 0;
constexpr int WAIT_TIME = 5;
bool g_Flag = false;

class SpatialFuzzer : public SpatialLocationCallbackStub {
public:
    SpatialFuzzer() {};
    virtual ~SpatialFuzzer() {};
    virtual void OnDirectionChanged(const DirectionResponse &directionRes) override;
    virtual void OnDistanceChanged(const DistanceResponse &distanceRes) override;
    virtual void OnAngleChanged(const AngleResponse &angleRes) override;
    virtual void OnInnerOuterChanged(const InnerOuterResponse &ioRes) override;
    virtual void OnExistChanged(const ExistResponse &existRes) override;
};

void SpatialFuzzer::OnInnerOuterChanged(const InnerOuterResponse &ioRes)
{
    cout << "inner and outer response: " << static_cast<int>(ioRes.innerOuterType);
    g_Flag = true;
}

void SpatialFuzzer::OnDistanceChanged(const DistanceResponse &distanceRes)
{
    cout << "distance response: " << distanceRes.distance;
    g_Flag = true;
}

void SpatialFuzzer::OnAngleChanged(const AngleResponse &angleRes)
{
    cout << "angle response: " << angleRes.angle;
    g_Flag = true;
}

void SpatialFuzzer::OnDirectionChanged(const DirectionResponse &directionRes)
{
    cout << "direction response: " << static_cast<int>(directionRes.directionType);
    g_Flag = true;
}

void SpatialFuzzer::OnExistChanged(const ExistResponse &existRes)
{
    cout << "exist response: " << static_cast<int>(existRes.existType);
    g_Flag = true;
}

namespace OHOS {
    bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
    {
        if (size >= FUZZ_DATA_LEN) {
            SpatialLocationMode mode1 = SpatialLocationMode::MODE_DIRECTION;
            SpatialLocationMode mode2 = SpatialLocationMode::MODE_DISTANCE;
            SpatialLocationMode mode3 = SpatialLocationMode::MODE_ANGLE;
            char deviceId[MAX_SIZE];
            char deviceName[MAX_SIZE];
            (void)memcpy_s(deviceId, sizeof(deviceId) / sizeof(deviceId[ZERO_DATA]
                data, MAX_SIZE);
            (void)memcpy_s(deviceName, sizeof(deviceName) / sizeof(deviceName[ZERO_DATA]),
                data, MAX_SIZE);
            SpatialDeviceType deviceType = static_cast<SpatialDeviceType>(data[FUZZ_FST_DATA]);
            SpatialDeviceInfo deviceInfo(std::to_string(deviceId[0]), std::to_string(deviceName[0]), deviceType);
            sptr<ISpatialLocationCallback> cb1 = new SpatialFuzzer();
            SpatialAwarenessMgrClient::GetInstance().SubscribeSpatialLocationRelationship(mode1,
                deviceInfo, cb1);
            SpatialAwarenessMgrClient::GetInstance().SubscribeSpatialLocationRelationship(mode2,
                deviceInfo, cb1);
            SpatialAwarenessMgrClient::GetInstance().SubscribeSpatialLocationRelationship(mode3,
                deviceInfo, cb1);
            sleep(WAIT_TIME);
            SpatialAwarenessMgrClient::GetInstance().UnSubscribeSpatialLocationRelationship(mode1,
                deviceInfo, cb1);
            SpatialAwarenessMgrClient::GetInstance().UnSubscribeSpatialLocationRelationship(mode2,
                deviceInfo, cb1);
            SpatialAwarenessMgrClient::GetInstance().UnSubscribeSpatialLocationRelationship(mode3,
                deviceInfo, cb1);
        }
        if (g_Flag) {
            return true;
        }
        return false;
    }
}

/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
    /* Run your code on data */
    OHOS::DoSomethingInterestingWithMyAPI(data, size);
    return 0;
}